<?php

namespace App\Http\Controllers\Admin;

use App\Admin\Albumlist;
use App\Admin\Photoalbum;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\DB;

class AlbumlistController extends Controller
{
    //
    public function list(){
        $data = DB::table('albumlist')
            ->join('photoalbum','photoalbum.id','=','albumlist.photo_id')
            ->where('albumlist.status','=',1)
            ->select('albumlist.*','photoalbum.cate_name')
            ->paginate(10);

        return view('admin.albumlist.list',compact('data'));
    }
    public function del(Request $request){
        $data =  $request->route( 'id' );
        Albumlist::where('id',$data)->update(['status'=>0]);
        return json_encode(['message'=>'删除成功']);
    }
    public function all_del(Request $request){
        $data =  $request->all();
        foreach ($data['id'] as $val){
//            echo $val.'<br>';
            DB::table('albumlist')->where('id',$val)->update(['status'=>0]);

        }
        return json_encode(['message'=>'删除成功']);
    }
    public function edit(Request $request){
        $data =  $request->route( 'id' );

        return view('admin/albumlist/edit',compact('data'));
    }
    public function upload(Request $request ){
        // 设置超时时间为没有限制
        ini_set("max_execution_time", "0");

        $http_type = ((isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';

        $file = $request->file('file');

        $allowed_extensions = ["png", "jpg", "gif", "jpeg", "bmp"];
        if ($file->getClientOriginalExtension() && !in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
            return json_encode(['error' => 'You may only upload png, jpg or gif or jpeg or bmp.']);
        }

        $destinationPath = 'storage/uploads/'.date('Ymd').'/'; //public 文件夹下面建 storage/uploads 文件夹
        $extension = $file->getClientOriginalExtension();
        $fileName = md5(microtime(true)).'.'.$extension;
        $file->move($destinationPath, $fileName);
        Cookie::queue('alb_img', $destinationPath.$fileName, 5);
    }

    public function edit_up( Request $request){
        if ($request->isMethod('post')){
            $data = $request->all();
            $user_img = Cookie::get('alb_img');

            if (!empty($user_img)){

                DB::table('albumlist')->where('id','=',$data['id'])
                    ->update(
                        [
                            'alb_img'=>Cookie::get('alb_img'),
                        ]
                    );
                Cookie::queue('alb_img',null,-1);
                return json_encode(['message'=>'修改成功1']);
            }
        }

    }
    public function add(){

        $data=Photoalbum::where('user_id',Auth::guard('admin')->user()->id)->get();

        return view('admin/albumlist/add',compact('data'));
    }
    public function add_up(Request $request){
        if ($request->isMethod('post')){
            $data = $request->all();
            unset($data['_token']);
            $data['alb_img'] = Cookie::get('alb_img');
            $data['created_at'] = date('Y-m-d H:i:s');
            DB::table('albumlist')->insert($data);
            Cookie::queue('alb_img',null,-1);
            return json_encode(['message'=>'添加成功']);
        }
    }
}
